package ru.beta2.testyard.engine.points;

import ru.beta2.testyard.ExpectMessageScript;
import ru.beta2.testyard.MessageListener;
import ru.beta2.testyard.engine.EventCallback;
import ru.beta2.testyard.engine.HotspotEvent;

/**
 * User: Inc
 * Date: 07.07.2008
 * Time: 6:58:43
 */
public class ExpectMessagePoint<T> extends ExpectPoint implements ExpectMessageScript<T>, EventCallback
{

    private T actualMessage;
    private MessageListener<T> messageListener;

    public ExpectMessagePoint(HotspotEvent expectedEvent)
    {
        super(expectedEvent);
    }

    public boolean isAcquired()
    {
        return actualMessage != null;
    }

    public T getActualMessage()
    {
        return actualMessage;
    }

    public void registerMessageListener(MessageListener<T> messageListener)
    {
        this.messageListener = messageListener;
    }

    public void eventReceived(HotspotEvent event)
    {
        actualMessage = (T) ((MessageEvent) event).getMessage();
        if (messageListener != null) {
            messageListener.messageAvailable(actualMessage);
        }
    }
}
